﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace cz.burger.windows.MDCCTaxi
{
    public class LocalProxy: IWebProxy
    {

        private WebProxyData Data = new WebProxyData();

        public Task<WebProxyData> Login()
        {
            return Task<WebProxyData>.Factory.StartNew(() => {
                this.Data.Logged = true;
                return this.Data;
            });
        }

        public async Task<WebProxyData> LoadStatus()
        {
            var statusFile = @"Assets\Status-Various.htm";
            var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            var file = await folder.GetFileAsync(statusFile);
            var stream = await file.OpenReadAsync();
            var reader = new StreamReader(stream.AsStreamForRead());
            var content = reader.ReadToEnd();
            var parser = new WebParser();

            List<string> rideTexts;
            if (!parser.GetRideStrings(content, out rideTexts))
            {
                Data.ErrorMessage = "Failed to parse the status page!";
            }
            else
            {
                foreach (var str in rideTexts)
                {
                    RideData ride;
                    if (parser.GetRide(str, out ride))
                    {
                        Data.Rides.Add(ride);
                    }
                }
            }

            return this.Data;
        }

    }
}
